/**
 * 演示程序当前的 “注册/登录” 等操作，是基于 “本地存储” 完成的
 * 当您要参考这个演示程序进行相关 app 的开发时，
 * 请注意将相关方法调整成 “基于服务端Service” 的实现。
 **/
// 野狗云端连接对象
var ref;
if(ref==undefined){
	ref= new Wilddog("https://postcard.wilddogio.com");
}
// 编写一个回调方法
function authDataCallback(authData) {
  if (authData) {
    console.log("User " + authData.uid + " is logged in with " + authData.provider);
    return authData.uid;
  } else {
    console.log("User is logged out");
  }
}
(function($, owner) {
	/**
	 * 用户登录
	 **/
	owner.login = function(loginInfo, callback) {
		callback = callback || $.noop;
		loginInfo = loginInfo || {};
		loginInfo.email = loginInfo.email || '';
		loginInfo.password = loginInfo.password || '';
		console.log("loginInfo.email " +loginInfo.email);
		console.log("loginInfo.password "+loginInfo.password);
		if (!checkEmail(loginInfo.email)) {
			return callback('邮箱地址不合法');
		}
		if (loginInfo.password.length < 6) {
			return callback('密码最短为 6 个字符');
		}
		var loginStr="";
		// 使用野狗云认证系统进行用户认证
		ref.authWithPassword({
				email: loginInfo.email,
				password: loginInfo.password
			},
			function(err, data) {
				if (err == null) {
					return owner.createState(loginInfo.email, callback);
				} else {
					if ('invalid_email' == err.code) {
						loginStr = "账号或密码错误";
					}else{
						loginStr = "登录失败！";
					}
				}
			});
		return callback(loginStr);
	};

	owner.createState = function(email, callback) {
		var state = owner.getState();
		state.account = email;
		state.token = "token123456789";
		owner.setState(state);
		return callback();
	};

	/**
	 * 新用户注册
	 **/
	owner.reg = function(regInfo, callback) {
		callback = callback || $.noop;
		regInfo = regInfo || {};
		regInfo.email = regInfo.email || '';
		regInfo.password = regInfo.password || '';
		var backStr = "";
		if (!checkEmail(regInfo.email)) {
			return callback('邮箱地址不合法');
		}
		if (regInfo.password.length < 6) {
			return callback('密码最短需要 6 个字符');
		}

		// 采用野狗用户认证系统进行用户注册
		ref.createUser({
				email: regInfo.email,
				password: regInfo.password
			},
			function(err, data) {
				if (err != null) {
					if (err.code == 'email_taken') {
						backStr = '邮箱已被注册！';
					}else{
						backStr = '注册失败！';
					}
				} else {
					backStr = "";
				}
		});
		return callback(backStr);
	};

	/**
	 * 获取当前状态
	 **/
	owner.getState = function() {
		var stateText = localStorage.getItem('$state') || "{}";
		return JSON.parse(stateText);
	};

	/**
	 * 设置当前状态
	 **/
	owner.setState = function(state) {
		state = state || {};
		localStorage.setItem('$state', JSON.stringify(state));
		//var settings = owner.getSettings();
		//settings.gestures = '';
		//owner.setSettings(settings);
	};

	var checkEmail = function(email) {
		email = email || '';
		return (email.length > 3 && email.indexOf('@') > -1);
	};

	/**
	 * 找回密码
	 **/
	owner.forgetPassword = function(email, callback) {
		callback = callback || $.noop;
		if (!checkEmail(email)) {
			return callback('邮箱地址不合法');
		}
		var credentials={email:email};
		ref.resetPassword (credentials,function(err){
			if (err!=null) {
				console.log(err.code);
			} else{
				return callback(null, '新的随机密码已经发送到您的邮箱，请查收邮件。');
			}
		});

	};
	/**
	 * 修改密码
	 **/
	owner.changePassword = function(credentials, callback) {
		callback = callback || $.noop;
		if (!checkEmail(credentials.email)) {
			return callback('邮箱地址不合法');
		}
		console.log(credentials);
		console.log(credentials.email);
		console.log(credentials.oldPassword);
		console.log(credentials.newPassword);
		ref.changePassword(credentials,function(err){
			if (err!=null) {
				console.log(err.code);
				return callback();
			} else{
				return callback('修改密码成功');
			}
		});

	};

	/**
	 * 获取应用本地配置
	 **/
	owner.setSettings = function(settings) {
		settings = settings || {};
		localStorage.setItem('$settings', JSON.stringify(settings));
	}

	/**
	 * 设置应用本地配置
	 **/
	owner.getSettings = function() {
		var settingsText = localStorage.getItem('$settings') || "{}";
		return JSON.parse(settingsText);
	}

}(mui, window.app = {}));